from django.db import models
from map.models import Map


class NPC(models.Model):
    name = models.CharField(max_length=64, verbose_name='npc名称')
    location = models.CharField(max_length=64, verbose_name='所在地')
    city = models.CharField(max_length=64,
                            choices=[(city.name, city.name) for city in Map.objects.all()],
                            verbose_name='所在城市')
    default_place = models.CharField(max_length=64, verbose_name='默认所在地')
    bio = models.TextField(max_length=1024, default="", verbose_name='npc简介')

    class Meta:
        verbose_name = 'npc'
        verbose_name_plural = verbose_name
        ordering = ['name']

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        city_places = Map.objects.get(name=self.city).places
        if self.default_place in city_places:
            if not self.location:
                self.location = self.default_place
            super().save(*args, **kwargs)
        else:
            raise ValueError("该城市没有%s这个地点！" % self.default_place)
